<!--页面配置添加与修改-->
<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增页面配置' : '编辑页面配置'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item prop="coverType" label="封面类型">
        <ren-radio-group v-model="dataForm.coverType" dict-type="coverType"></ren-radio-group>
      </el-form-item>
      <el-form-item prop="pageName" label="页面名称">
        <el-input v-model="dataForm.pageName" placeholder="请输入页面名称"></el-input>
      </el-form-item>
      <el-form-item prop="pageLocation" label="页面位置">
        <ren-select v-model="dataForm.pageLocation" dict-type="pageLocation" placeholder="请选择页面位置"></ren-select>
      </el-form-item>
      <el-form-item prop="pageParam" label="页面参数">
        <el-select v-model="dataForm.pageParam" placeholder="请选择参数" clearable filterable>
          <el-option :label="data.label" v-for="data in selectList" :key="data.value" :value ="data.value">{{data.label}}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="pagePath" label="页面路径">
        <ren-select v-model="dataForm.pagePath" dict-type="pagePath" placeholder="请选择页面路径"></ren-select>
      </el-form-item>
      <el-form-item prop="imageUrl" label="图片" v-if="dataForm.coverType==='图像'">
        <div class="flex_center flex_wrap">
          <div class="img_con" v-if="dataForm.imageUrl">
            <el-image class="deduct_img" :src="dataForm.imageUrl" :preview-src-list="[dataForm.imageUrl]"/>
            <i class="el-icon-error remove_img" @click="cancelImg(dataForm.imageUrl)"/>
          </div>
          <com-upload v-show="!dataForm.imageUrl" @get-url="getUrlOne"></com-upload>
        </div>
        <div style="color:#909399;" v-if="prompt"><el-icon class="el-icon-info"></el-icon>{{prompt}}</div>
      </el-form-item>
      <el-form-item prop="videoUrl" label="视频" v-if="dataForm.coverType==='视频'">
        <div class="flex_center flex_wrap">
          <div class="img_con" v-if="dataForm.videoUrl">
<!--            <el-image class="deduct_img" :src="dataForm.videoUrl" :preview-src-list="[dataForm.videoUrl]"/>-->
<!--            <i class="el-icon-error remove_img" @click="cancelImg(dataForm.videoUrl)"/>-->
            <video :src="dataForm.videoUrl" controls="controls" width="320" height="240"></video>
            <i class="el-icon-error remove_img" @click="cancelImg(dataForm.videoUrl)"/>
          </div>
          <com-upload v-show="!dataForm.videoUrl" @get-url="getUrlTwo"></com-upload>
        </div>
        <div style="color:#909399;" v-if="prompt"><el-icon class="el-icon-info"></el-icon>{{prompt}}</div>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import {getDictDataList} from "@/utils";

export default {
  data() {
    return {
      visible: false,
      prompt:'',//图片提示文本
      promptList:['建议上传670x375的图片','建议上传750宽的图片','建议上传750宽的图片','建议上传600x200的图片'],           //图片提示文本数组
      productList:[],            //所有商品数据
      articleList:[],            //所有文章数据
      groupList:[],              //所有分类数据
      selectList:[],             //下拉选择数据
      dataForm: {
        id: '',
        imageUrl: '',            //页面外观
        pageLocation: '',        //页面位置
        pageName: '',            //页面名称
        pageParam: '',           //页面参数
        pagePath: '',	           //页面路径
        coverType: '图像',	         //封面类型
        videoUrl: '',	           //视频地址
      }
    }
  },
  computed: {
    dataRule() {
      return {
        coverType: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        imageUrl: [
          {required: this.dataForm.coverType==='图像' ? true:false, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        videoUrl: [
          {required: this.dataForm.coverType==='视频' ? true:false, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        pageLocation: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        pageName: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        pageParam: [
          {required: this.dataForm.pagePath==='pages/index/goodsDetailed' || this.dataForm.pagePath==='pages/index/shopArticleDetailed'|| this.dataForm.pagePath==='pages/order/selectWine'?true:false, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        pagePath: [
          {required: this.dataForm.pageParam ? true:false, message: this.$t('validate.required'), trigger: 'blur'}
        ],
      }
    }
  },
  watch:{
    // 监听表单数据，防止父组件数据变化影响本组件
    'dataForm.pageLocation'(newValue, oldValue){
      if (newValue==='1'){
        this.prompt = this.promptList[0]
      }
      if (newValue==='7'){
        this.prompt = this.promptList[1]
      }
      if (newValue==='8'){
        this.prompt = this.promptList[2]
      }
      if (newValue==='9'){
        this.prompt = this.promptList[3]
      }
      if (!newValue){
        this.prompt = ''
      }
    },
    'dataForm.pagePath'(newValue, oldValue){
      console.log(newValue)
      let _this = this
      if (newValue==='pages/index/goodsDetailed'||newValue==='pages/order/selectWine'){
        setTimeout(function() {
          _this.selectList = _this.productList.map((item)=>{
            return {
              value: item.id,
              label: item.productName
            }
          })
        }, 500)
      }
      if (newValue==='pages/index/shopArticleDetailed'){
        setTimeout(function() {
          _this.selectList = _this.articleList.map((item)=>{
            return {
              value: item.id,
              label: item.tile
            }
          })
        }, 500)
      }
      if (newValue==='pages/index/classification'){
        setTimeout(function() {
          _this.selectList = _this.groupList.map((item)=>{
            return {
              value: item.id,
              label: item.groupName
            }
          })
        }, 500)
      }
      if (this.dataForm.pagePath==='pages/index/caseList'){
        this.selectList = getDictDataList('category').map((item)=>{
          return {
            value: item.dictValue,
            label: item.dictLabel
          }
        })
      }
      this.dataForm.pageParam = this.dataForm.id? this.dataForm.pageParam :  ''
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.getGroup()//获取商品信息
        this.getArticle()//获取文章列表
        this.getProductGroup()//获取分类信息
        if (this.dataForm.id) {
          this.getInfo()
        }
        this.dataForm.imageUrl = this.dataForm.id? this.dataForm.imageUrl : ''
        this.dataForm.videoUrl = this.dataForm.id? this.dataForm.videoUrl : ''
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/configPage/${this.dataForm.id}`).then(({data: res}) => {
        //console.log(res)
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        if (this.dataForm.pagePath==='pages/index/goodsDetailed'||this.dataForm.pagePath==='pages/order/selectWine'){
          this.selectList = this.productList.map((item)=>{
            return {
              value: item.id,
              label: item.productName
            }
          })
          //console.log(this.dataForm)
        }
        if (this.dataForm.pagePath==='pages/index/shopArticleDetailed'){
          this.selectList = this.articleList.map((item)=>{
            return {
              value: item.id,
              label: item.tile
            }
          })
        }
        if (this.dataForm.pagePath==='pages/index/classification'){
          this.selectList = this.groupList.map((item)=>{
            return {
              value: item.id,
              label: item.groupName
            }
          })
        }
        if (this.dataForm.pagePath==='pages/index/caseList2'){
          this.selectList = getDictDataList('category').map((item)=>{
            return {
              value: item.dictValue,
              label: item.dictLabel
            }
          })
        }
      }).catch(() => {
      })
    },
    // 获取商品信息
    getGroup() {
      this.$http.get(`/api/product/page`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.productList = res.data.list
        //console.log(this.productList)
      }).catch(() => {})
    },
    // 获取文章信息
    getArticle() {
      this.$http.get(`/api/shopArticle/page`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.articleList = res.data.list
        //console.log(this.articleList)
      }).catch(() => {})
    },
    // 获取分类信息
    getProductGroup() {
      this.$http.get(`/api/productGroup/page`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.groupList = res.data.list
        // console.log(this.groupList)
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/api/configPage', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false}),
    // 添加单图添加
    getUrlOne(getUrl){
      this.dataForm.imageUrl = getUrl
    },
    // 添加单视频添加
    getUrlTwo(getUrl){
      this.dataForm.videoUrl = getUrl
    },
    // 删除服务器图片
    cancelImg(url) {
      this.$http({
        url: `/sys/oss/deleteByUrl`,
        method: 'delete',
        data: {url: url},
      }).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm.imageUrl = ''
        this.dataForm.videoUrl = ''
        if (this.dataForm.id){
          this.$http['put']('/api/configPage', this.dataForm).then(({data: res}) => {
          }).catch((err) => {
            this.$message.error(err.msg)
          })
        }
      }).catch(() => {
      })
    },
  }
}
</script>
<style>
.el-upload el-upload-picture-card {
  display: none;
}

.img_con {
  position: relative;
  display: flex;
}

.deduct_img {
  width: 140px;
  height: 140px;
  margin-right: 10px;
  margin-bottom: 10px;
}

.remove_img {
  position: absolute;
  right: 5px;
  top: -5px;
  color: red;
  cursor: pointer;
}
</style>
